package com.example.myapplication.audio;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.myapplication.R;
import com.example.myapplication.bean.AudioBean;
import com.example.myapplication.databinding.AudioLayoutBinding;

import java.io.IOException;
import java.util.List;

public class AudioListAdapter extends BaseAdapter {
    private Context context;//定义上下文
    private List<AudioBean> mData;//每一个数据对象

    public AudioListAdapter(Context context) {
        this.context = context;
    }

    public interface OnItemPlayClickListener {
        void onItemPlayClick(AudioListAdapter adapter, View convertView, View playView, int position) throws IOException;
    }
    private OnItemPlayClickListener onItemPlayClickListener;
    public void setOnItemPlayClickListener(OnItemPlayClickListener onItemPlayClickListener) {
        this.onItemPlayClickListener = onItemPlayClickListener;
    }
    public AudioListAdapter(Context context, List<AudioBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.audio_layout, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AudioBean audioBean = mData.get(position);
        viewHolder.ab.tvTime.setText(audioBean.getTime());
        viewHolder.ab.tvDuration.setText(audioBean.getDuration());
        viewHolder.ab.tvTitle.setText(audioBean.getTitle());
        if (audioBean.isPlaying()) {//currently is playing
            viewHolder.ab.llController.setVisibility(View.VISIBLE);//显示
            viewHolder.ab.pbPlay.setMax(100);
            viewHolder.ab.pbPlay.setProgress(audioBean.getProgress());
            viewHolder.ab.ivPlay.setImageResource(R.drawable.ic_pause_white);
        } else {
            viewHolder.ab.ivPlay.setImageResource(R.drawable.ic_play_arrow_white);//when stopped or paused shows the other img

            if (!audioBean.isPaused()){
                viewHolder.ab.llController.setVisibility(View.GONE);//yincang
            }
        }
        View finalConvertView = convertView;
        viewHolder.ab.ivPlay.setOnClickListener(new View.OnClickListener() {//点击播放图标实现控制
            @Override
            public void onClick(View v) {
                if (onItemPlayClickListener != null) {
                    try {
                        onItemPlayClickListener.onItemPlayClick(AudioListAdapter.this, finalConvertView, v, position);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        AudioLayoutBinding ab;
        public ViewHolder(View v) {
            ab = AudioLayoutBinding.bind(v);
        }
    }

}
